#include "log.h"

const char *gLevelMap[] = {
    "DEBUGE",
    "NORMAL",
    "WARING",
    //"ERROR",
    "FATAL"};

void LogMessage(int level, const char *file, int line, const char *format, ...)
{

    // 日志标准部分
    char stdbuffer[1024];
    time_t get_time = time(nullptr);
    struct tm *info = localtime(&get_time);
    snprintf(stdbuffer, sizeof(stdbuffer), "[%s]|[%d-%02d-%02d %02d:%02d:%02d]",
             gLevelMap[level],
             info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
             info->tm_hour, info->tm_min, info->tm_sec);

    // 日志自定义部分
    char logbuffer[1024];
    va_list valist;
    va_start(valist, format);
    vsnprintf(logbuffer, sizeof(logbuffer), format, valist);
    va_end(valist);
    // 截取文件名部分
    const char* fileName = strrchr(file, '/');
    if (fileName) {
        fileName++;
    } else {
        fileName = file;
    }
    // 文件名和行号
    char fileInfo[256];
    snprintf(fileInfo, sizeof(fileInfo), "[%s:%d]", fileName, line);

    // 打印完整的日志信息到文件
    printf("%s %s %s\n", stdbuffer, fileInfo, logbuffer);
    // FILE* fp = fopen(FILE_NAME, "a+");
    // if (fp != nullptr) {
    //     fprintf(fp, "%s %s %s\n", stdbuffer, fileInfo, logbuffer);
    //     fclose(fp);
    // }
}
